package com.urlshortener

class GoGlURL {



	static String shorten(String apikey,String longUrl){


		HttpURLConnection connection = null;

		def addr="https://www.googleapis.com/urlshortener/v1/url"

		def url = addr.toURL()
		connection = (HttpURLConnection)url.openConnection();
		connection.setRequestMethod("POST");
		connection.setUseCaches (false);
		connection.setDoInput(true);
		connection.setDoOutput(true);

		DataOutputStream wr = new DataOutputStream (
				connection.getOutputStream ());
		wr.writeBytes ("longUrl": "${longUrl}");
		wr.flush ();
		wr.close ();


		InputStream is = connection.getInputStream();
		BufferedReader rd = new BufferedReader(new InputStreamReader(is));
		String line;
		StringBuffer xresponse = new StringBuffer();
		while((line = rd.readLine()) != null) {
			xresponse.append(line);
			xresponse.append('\r');
		}
		rd.close();
		return xresponse.toString();
	}
}
